//
//  EditProjectViewController.m
//  OAProject
//
//  Created by yhj on 2017/3/28.
//  Copyright © 2017年 cdnunion. All rights reserved.
//  编辑项目主界面

#import "EditProjectViewController.h"

@interface EditProjectViewController ()<UITextViewDelegate>

@property(nonatomic,strong)UIView *mainView;

@property(nonatomic,strong)UITextView *textView;

@end

@implementation EditProjectViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor=WhiteColor;

    self.automaticallyAdjustsScrollViewInsets=NO;

    self.title=@"修改项目";

    [self.view addSubview:self.mainView];

    // 确认提交
    [AppUI initNavigationItem:self.navigationItem andWithText:@"确  认" andTextColor:WhiteColor andIsRight:YES withBlock:^{

            [API EditProjectWithProjectID:self.model.projectID description:self.textView.text success:^(id res) {

                if ([res[@"rt"] isEqual:@(1)])
                {
                    [self.view makeToast:@"修改成功" duration:1 position:CSToastPositionCenter];

                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        
                        [self.navigationController popViewControllerAnimated:NO];

                    });
                }
                else
                {
                 [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
                }

            } failure:^(NSInteger stateCode) {

            }];
    }];
}

-(UIView *)mainView
{
    if (!_mainView) {
        _mainView=[UIView new];
        _mainView=[[UIView alloc]initWithFrame:CGRectMake(0,64,APPW,0)];
        _mainView.backgroundColor=WhiteColor;
        setViewHeight_Add(_mainView,kmargin*2);

        // 项目名称
        UILabel *projectNameLabel=[self.mainView getLabelWithText:@"项目名称(不能修改)" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.mainView addSubview:projectNameLabel];
        projectNameLabel.frame=CGRectMake(kmargin*2,_mainView.frame.size.height,APPW/2,kmargin*2);
        setViewHeight_Add(_mainView,kmargin*2);

        UILabel *projectNameDetailLabel=[self.mainView getLabelWithText:self.model.project_name font:Font_Number(16) textColor:LightGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.mainView addSubview:projectNameDetailLabel];
        projectNameDetailLabel.frame=CGRectMake(kmargin*2,_mainView.frame.size.height+kmargin/2,APPW/2,kmargin*2);
        setViewHeight_Add(_mainView,kmargin*4);

        // 项目描述
        UILabel *projectDesLabel=[self.mainView getLabelWithText:@"项目描述" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.mainView addSubview:projectDesLabel];
        projectDesLabel.frame=CGRectMake(kmargin*2,_mainView.frame.size.height,APPW/2,kmargin*2);
        setViewHeight_Add(_mainView,kmargin*2);


        _textView=[[UITextView alloc]initWithFrame:CGRectMake(kmargin*3/2,_mainView.frame.size.height,APPW-kmargin*3,APPH/5)];
        _textView.keyboardType=UIKeyboardTypeDefault;
        _textView.text=self.model.descriptionStr;
        _textView.textColor=DarkGreyColor;
        _textView.delegate=self;
        _textView.font=Font_Number(16);
        [_textView becomeFirstResponder];
        [self.mainView addSubview:_textView];
        setViewHeight_Add(_mainView,APPH/5);
    }
    return _mainView;
}

// 点击return键时候键盘下去
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

@end
